//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.liy.framework.common.office;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class FreeMarkerUtil {
    private static Configuration cfg;

    private FreeMarkerUtil() {
    }

    public static FreeMarkerUtil getInstance(String templatePath) throws IOException {
        if (null == cfg) {
            cfg = FreeMarkerUtil.LazyHolder.config;
            cfg.setDefaultEncoding("UTF-8");
            cfg.setDirectoryForTemplateLoading(new File(templatePath));
        }

        return FreeMarkerUtil.LazyHolder.fk;
    }

    public static FreeMarkerUtil getNewTemplateInstance(String templatePath) throws IOException {
        if (null == cfg) {
            cfg = FreeMarkerUtil.LazyHolder.config;
            cfg.setDefaultEncoding("UTF-8");
        }

        cfg.setDirectoryForTemplateLoading(new File(templatePath));
        return FreeMarkerUtil.LazyHolder.fk;
    }

    private Template getTemplate(String templateName) throws Exception {
        return cfg.getTemplate(templateName);
    }

    public String getContent(Map<String, Object> dataModel, String templateName) throws Exception {
        StringWriter stringWriter = new StringWriter();
        this.getTemplate(templateName).process(dataModel, stringWriter);
        stringWriter.flush();
        String result = stringWriter.toString();
        stringWriter.close();
        return result;
    }

    public File process(Map<String, Object> dataModel, String templateName, String filePath) throws Exception {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        BufferedWriter bufWrite = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
        this.getTemplate(templateName).process(dataModel, bufWrite);
        bufWrite.flush();
        bufWrite.close();
        return file;
    }

    public String getImgBase64ByUrl(String strUrl) {
        try {
            HttpURLConnection conn = (HttpURLConnection)(new URL(strUrl)).openConnection();
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            if (conn.getResponseCode() == 200) {
                InputStream inStream = conn.getInputStream();
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                boolean var6 = false;

                int len;
                while((len = inStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }

                byte[] btImg = outStream.toByteArray();
                inStream.close();
                outStream.flush();
                outStream.close();
                return new String(Base64.encode(btImg));
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return null;
    }

    private static class LazyHolder {
        private static final Configuration config;
        private static final FreeMarkerUtil fk;

        private LazyHolder() {
        }

        static {
            config = new Configuration(Configuration.VERSION_2_3_23);
            fk = new FreeMarkerUtil();
        }
    }
}
